package nc.oa.login;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.codec.binary.Base64;

import nc.bs.logging.Logger;

public class SSONCCLoginUtil {
	
	public String getToken(SSOLoginParam param) throws IOException {
		String user_code = param.getUsercode();//url传过来的用户编码
		Logger.info("##SSO-getToken::user_code:" + user_code);
		
		String dsname = param.getDatasource(); //数据源名称
		String usercode = user_code != null && user_code.length() > 0 ? user_code : param.getUsercode();//nccy用户编码
		String client_id = param.getThirdsys_client_id();//第三方系统client_id
		String client_security = param.getThirdsys_client_security();//第三方秘钥client_security
		String langcode = param.getLangcode();//多语语种
		String busicentercode = param.getBusicentercode() ;//账套编码
		String groupcode = "";//集团编码
		
		URL preUrl = new URL(param.getErp_url() + ISSOLoginUtils.URI);
		Logger.info("##SSO-getToken::url:" + preUrl);
		
		URLConnection uc = preUrl.openConnection();
		uc.setDoOutput(true);
		uc.setUseCaches(false);
		uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		uc.setRequestProperty("Content-Length","10000" );
		uc.setRequestProperty("userid", param.getUsercode());
		HttpURLConnection hc = (HttpURLConnection) uc;
		hc.setRequestMethod("POST");
		OutputStream os = null;
		DataOutputStream dos = null;
		String returnFlag = "";
		InputStream is = null;
		long ts =System.currentTimeMillis();
		try {
			StringBuffer sb = new StringBuffer();
			String security = genKey(usercode,usercode + client_security + String.valueOf(ts).substring(0, 6));
			//security=URLEncoder.encode(security ,"UTF-8");
			sb.append("busicentercode="+busicentercode+"&type=type_security&dsname="+dsname+ "&usercode="+usercode+ "&client_id="+client_id+ "&security="+security);

			String s=sb.toString().replaceAll("\\+", "%2B");
			Logger.info("##SSO-getToken::str:" + s);
			os = hc.getOutputStream();
			dos = new DataOutputStream(os);
			dos.write(s.getBytes());	
			dos.close();
			dos.flush();

			is = hc.getInputStream();
			int ch;
			while ((ch = is.read()) != -1) {
				//获取的系统token值
				returnFlag += String.valueOf((char) ch);
			}
			
			Logger.info("##SSO-getToken::token:" + returnFlag);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (Exception e2) {
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (Exception e2) {
				}
			}
			if (is != null)
				try {
					is.close();
				} catch (Exception e2) {
			}
		}
		return returnFlag;
	} 
	
	private static String genKey(String userid,String key) throws Exception{
		return new Base64().encodeToString(SignatureTookKit.digestSign(userid.getBytes(), key.getBytes()));
		
	}

}